package com.yxx.community.service;

import com.yxx.community.dto.NotificationDTO;
import com.yxx.community.dto.PaginationDTO;
import com.yxx.community.dto.QuestionDTO;
import com.yxx.community.enums.NotificationEnum;
import com.yxx.community.enums.NotificationStatusEnum;
import com.yxx.community.exception.CustomizeErrorCode;
import com.yxx.community.exception.CustomizeException;
import com.yxx.community.mapper.CommentMapper;
import com.yxx.community.mapper.NotificationExtMapper;
import com.yxx.community.mapper.NotificationMapper;
import com.yxx.community.mapper.UserMapper;
import com.yxx.community.model.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: YuXinXin
 * @Description:
 * @Date:Created in 18:39 2020/2/13
 **/
@Service
public class NotificationService {

  @Autowired
  private NotificationMapper notificationMapper;
  @Autowired
  private NotificationExtMapper notificationExtMapper;
  @Autowired
  private CommentMapper commentMapper;

  public PaginationDTO listByUserId(Long userId, Integer page, Integer size) {
    PaginationDTO<NotificationDTO> paginationDTO=new PaginationDTO<> ();
    NotificationExample notificationExample = new NotificationExample();
    notificationExample.createCriteria().andReceiverEqualTo(userId);
    Integer totalCount = notificationMapper.countByExample(notificationExample);
    paginationDTO.setPagination(totalCount,page,size);

    Integer offset=size*(page-1);
    List<Notification> notifications = notificationExtMapper.listByUserId(userId,offset,size);
    if(notifications.size()==0){
      return paginationDTO;
    }
    List<NotificationDTO> notificationDTOS=new ArrayList<>();
    for (Notification notification : notifications) {
      if(notification.getType()==NotificationEnum.REPLY_COMMENT.getType()){
        Comment comment = commentMapper.selectByPrimaryKey(notification.getOuterId());
        notification.setOuterTitle(comment.getContent());
      }
      NotificationDTO notificationDTO = new NotificationDTO();
      BeanUtils.copyProperties(notification,notificationDTO);
      notificationDTO.setTypeName(NotificationEnum.nameOfType(notification.getType()));
      notificationDTOS.add(notificationDTO);
    }
    paginationDTO.setData(notificationDTOS);
    return paginationDTO;
  }

  public int ureadCount(Long id) {
    NotificationExample example = new NotificationExample();
    example.createCriteria().andReceiverEqualTo(id).andStatusEqualTo(NotificationStatusEnum.UREAD.getStatus());
    return notificationMapper.countByExample(example);
  }

  public NotificationDTO read(Long id, User user) {
    Notification notification = notificationMapper.selectByPrimaryKey(id);
    if(notification==null){
      throw new CustomizeException(CustomizeErrorCode.NOTIFICATION_NOT_FIND);
    }
    notification.setStatus(NotificationStatusEnum.READ.getStatus());
    notificationMapper.updateByPrimaryKey(notification);
    if(notification.getType()==NotificationEnum.REPLY_COMMENT.getType()){
      Comment comment = commentMapper.selectByPrimaryKey(notification.getOuterId());
      notification.setOuterId(comment.getParentId());
    }
    NotificationDTO notificationDTO = new NotificationDTO();
    BeanUtils.copyProperties(notification,notificationDTO);
    notificationDTO.setTypeName(NotificationEnum.nameOfType(notification.getType()));
    return notificationDTO;
  }
}
